#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <pthread.h>

#define BUF_SIZE 1024
#define PORT 8888
#define THREAD_POOL_SIZE 10
#define QUEUE_SIZE 10

// 定义链表节点结构体
typedef struct node
{
    void *data;
    struct node *next;
} node_t;

// 定义队列结构体
typedef struct
{
    node_t *head;
    node_t *tail;
    int size;
    pthread_mutex_t lock;
    pthread_cond_t not_empty;
    pthread_cond_t not_full;
} queue_t;

// 初始化队列
void queue_init(queue_t *q)
{
    q->head = NULL;
    q->tail = NULL;
    q->size = 0;
    pthread_mutex_init(&q->lock, NULL);
    pthread_cond_init(&q->not_empty, NULL);
    pthread_cond_init(&q->not_full, NULL);
}

// 销毁队列
void queue_destroy(queue_t *q)
{
    pthread_mutex_destroy(&q->lock);
    pthread_cond_destroy(&q->not_empty);
    pthread_cond_destroy(&q->not_full);
    node_t *curr = q->head;
    while (curr != NULL)
    {
        node_t *temp = curr;
        curr = curr->next;
        free(temp);
    }
}

// 入队
void queue_push(queue_t *q, void *data)
{
    node_t *node = (node_t *)malloc(sizeof(node_t));
    node->data = data;
    node->next = NULL;
    pthread_mutex_lock(&q->lock);
    while (q->size >= QUEUE_SIZE)
    {
        pthread_cond_wait(&q->not_full, &q->lock);
    }
    if (q->head == NULL)
    {
        q->head = node;
        q->tail = node;
    }
    else
    {
        q->tail->next = node;
        q->tail = node;
    }
    q->size++;
    pthread_cond_signal(&q->not_empty);
    pthread_mutex_unlock(&q->lock);
}

// 出队
void *queue_pop(queue_t *q)
{
    pthread_mutex_lock(&q->lock);
    while (q->size == 0)
    {
        pthread_cond_wait(&q->not_empty, &q->lock);
    }
    node_t *node = q->head;
    void *data = node->data;
    q->head = node->next;
    if (q->head == NULL)
    {
        q->tail = NULL;
    }
    free(node);
    q->size--;
    pthread_cond_signal(&q->not_full);
    pthread_mutex_unlock(&q->lock);
    return data;
}

// 线程函数，用于处理队列中的数据
void *process_data(void *arg)
{
    queue_t *q = (queue_t *)arg;
    while (1)
    {
        void *data = queue_pop(q);
        // 处理数据的代码
        printf("data: %s\n", (char *)data);
        free(data);
    }
    return NULL;
}

int main()
{
    int sockfd, ret;
    struct sockaddr_in servaddr, cliaddr;
    char buf[BUF_SIZE];

    // 创建UDP套接字
    sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (sockfd < 0)
    {
        perror("socket");
        exit(1);
    }

    // 设置服务器地址结构
    memset(&servaddr, 0, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servaddr.sin_port = htons(PORT);

    // 绑定服务器地址结构到UDP套接字
    ret = bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
    if (ret < 0)
    {
        perror("bind");
        exit(1);
    }

    // 初始化队列和线程池
    queue_t queue;
    queue_init(&queue);
    pthread_t threads[THREAD_POOL_SIZE];
    for (int i = 0; i < THREAD_POOL_SIZE; i++)
    {
        pthread_create(&threads[i], NULL, process_data, (void *)&queue);
    }

    // 循环接收客户端数据并加入队列
    while (1)
    {
        socklen_t len = sizeof(cliaddr);
        ret = recvfrom(sockfd, buf, BUF_SIZE, 0, (struct sockaddr *)&cliaddr, &len);
        if (ret < 0)
        {
            perror("recvfrom");
            continue;
        }
        buf[ret] = '\0';
        char *data = strdup(buf);
        queue_push(&queue, data);
    }

    // 销毁队列和线程池
    queue_destroy(&queue);
    for (int i = 0; i < THREAD_POOL_SIZE; i++)
    {
        pthread_join(threads[i], NULL);
    }

    // 关闭套接字
    close(sockfd);

    return 0;
} 